package com.uenpay.im.ui.old.activity.dynamic;

import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.invokeitems.BaseHttpInvokeItem;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


/**
 * get personal dynamics
 * 
 * @author SL
 * @copyRight http://www.qiyunxin.com
 * 
 */
public class getDynamicByCustInvokeItem extends BaseHttpInvokeItem {
	public class getDynamicByCustInvokeItemResult extends HttpInvokeResult {
		public ArrayList<DynamicEntity> dynamics;
		public String dynamicBgFileId;
		public String dynamicBgFileHash;
	}

	public getDynamicByCustInvokeItemResult getOutPut() {
		return (getDynamicByCustInvokeItemResult) GetResultObject();
	}

	public getDynamicByCustInvokeItem(int page, String cust_id) {
		String url = APIConfiguration.getBaseUrl()
				+ "Dynamic/Dynamic/getDynamicByCust?pageIndex=" + page
				+ "&pageSize=12&custid=" + cust_id + "&"
				+ APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		getDynamicByCustInvokeItemResult result = new getDynamicByCustInvokeItemResult();
		JSONObject json = null;
		try {
			json = new JSONObject(responseBody);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		if (json == null) {
			return null;
		}
		result.status = json.optInt("status");
		result.msg = json.optString("msg");
		JSONObject jsonData = json.optJSONObject("data");
		if (jsonData != null) {
			JSONObject dynamicSetting = jsonData
					.optJSONObject("dynamicsetting");
			if (dynamicSetting != null) {
				result.dynamicBgFileHash = dynamicSetting
						.optString("bgfilehash");
				result.dynamicBgFileId = dynamicSetting.optString("bgfileid");
			}
		}
		
		if(jsonData != null && jsonData.has("list")){
			JSONArray array = jsonData.optJSONArray("list");
			if (array != null) {
				result.dynamics = DynamicSerializer.deserializeDynamicEntitys(array);
			}
		}
		

		return result;
	}
}
